package com.wzy.dongtai;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/08/29/14:02
 * @Description:
 */
public class day07 {

    @Test
    void test() {
        int[] arr = {7, 1, 5, 3, 6, 4};
        System.out.println(maxProfitAndRule(arr));
    }

    public int maxProfit(int[] prices) {
        int min = prices[0];
        int max = 0;
        for (int i = 1; i < prices.length; i++) {
            max = Math.max(max, prices[i] - min);
            min = Math.min(min, prices[i]);
        }
        return max;
    }

    public int maxProfit1(int[] prices) {
       /* if (prices == null || prices.length <= 1){
            return 0;
        }
        int sum = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i]-prices[i-1]>0){
                sum += prices[i]-prices[i-1];
            }
        }
        return sum;*/
        //exaample 2
       /* int n = prices.length;
        int[][] dp = new int[n][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);
        }
        return dp[n-1][0];*/
        //优化
        int n = prices.length;
        int dp0 = 0, dp1 = -prices[0];
        for (int i = 1; i < n; i++) {
            int newDp0 = Math.max(dp0, dp1 + prices[i]);
            int newDp1 = Math.max(dp1, dp0 - prices[i]);
            dp0 = newDp0;
            dp1 = newDp1;
        }
        return dp0;

    }

    /*
        分析
        我们目前持有一支股票，对应的「累计最大收益」记为 f[i][0]；
        我们目前不持有任何股票，并且处于冷冻期中，对应的「累计最大收益」记为 f[i][1]；
        我们目前不持有任何股票，并且不处于冷冻期中，对应的「累计最大收益」记为 f[i][2]。
     */
    public int maxProfitAndRule(int[] prices) {
        if (prices == null || prices.length <= 1)
            return 0;
        int n = prices.length;
        int[][] db = new int[n][3];
        db[0][0] = -prices[0];
        db[0][1] = 0;
        db[0][2] = 0;
        for (int i = 1; i < n; i++) {
            db[i][0] = Math.max(db[i - 1][2] - prices[i], db[i - 1][0]);
            db[i][2] = Math.max(db[i - 1][1], db[i - 1][2]);
            db[i][1] = db[i - 1][0] + prices[i];
        }

        return db[n - 1][1];
//        return Math.max(db[n-1][1],db[n-1][2]);
    }

    @Test
    void test2() {
        String str = "hello";
        String s = "el";
        String s1 = str.substring(0, str.indexOf(s));
        String s2 = str.substring(str.indexOf(s) + s.length());
        System.out.println(str);
        System.out.println(s1);
        System.out.println(s2);
        Set<String> set = new HashSet();
        List<String> list = new ArrayList<>(set);
        List<String> collect = list.stream().distinct().collect(Collectors.toList());
        for (int i = 0; i < collect.size(); i++) {
            System.out.println(collect.get(0));
        }

    }

}
